package com.aoip.telemetry.telemetry.configureui.business.analysis.access.controller;

import com.aoip.telemetry.telemetry.configureui.business.analysis.access.service.StationAccessService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.configureui.common.utils.poi.ExcelUtil;
import com.aoip.telemetry.telemetry.entity.equipment.StInfoConfigEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 测站接入模块
 *
 * @author chenbai
 * @version 1.0
 * @date 2021/12/27 10:45
 */
@RestController
public class StationAccessController extends BaseController {

    @Resource
    private StationAccessService service;

    /**
     * 获取接入测站配置分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.configureui.common.core.list.TableDataInfo
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/station/access/page")
    public TableDataInfo page(StInfoConfigEntity entity) {
        startPage();
        List<StInfoConfigEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 获取单个接入测站详情
     *
     * @param stCd 测站编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/station/access/{stCd}")
    public AjaxResult station(@PathVariable String stCd) {
        return AjaxResult.success(service.station(stCd));
    }

    /**
     * 查询所有接入测站
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @GetMapping("/v1/GET/station/access/list")
    public AjaxResult list(StInfoConfigEntity entity) {
        // todo 数据量大时，后续可以查询redis 提升查询效率
        List<StInfoConfigEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 新增接入测站
     */
    @Log(title = "接入测站", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/station/access")
    public AjaxResult add(@Validated @RequestBody StInfoConfigEntity entity) {
        String stCd = entity.getStCd();
        StInfoConfigEntity station = service.station(stCd);
        if (station != null) {
            return AjaxResult.error("新增测站'" + stCd + "'失败，该测站已存在。");
        }
        return toAjax(service.insert(entity));
    }

    /**
     * 修改接入测站
     */
    @Log(title = "接入测站", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/station/access")
    public AjaxResult edit(@Validated @RequestBody StInfoConfigEntity entity) {
        String stCd = entity.getStCd();
        StInfoConfigEntity station = service.station(stCd);
        if (station == null) {
            return AjaxResult.error("修改测站'" + stCd + "'失败，该测站不存在。");
        }
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（stCode, stInfoConfigEntity）,（测站编码，测站实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/station/access/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除接入测站
     */
    @Log(title = "接入测站", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/station/access/{stCds}")
    public AjaxResult remove(@PathVariable String[] stCds) {
        service.delete(stCds);
        return success();
    }

    @Log(title = "接入测站", businessType = BusinessType.EXPORT)
    @PostMapping("/v1/POST/station/access/export")
    public void export(HttpServletResponse response, StInfoConfigEntity entity) {
        List<StInfoConfigEntity> list = service.list(entity);
        ExcelUtil<StInfoConfigEntity> util = new ExcelUtil<>(StInfoConfigEntity.class);
        util.exportExcel(response, list, "接入测站数据");
    }
}
